#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAX_NODE = 110;
struct node{
	int ptr,lch,rch;
};
node tree[MAX_NODE];

void preorder(int t){
	if(t!=0){
		printf("%d ",t);
		preorder(tree[t].lch);
		preorder(tree[t].rch);
		
	}
}

void inorder(int t){
	if(t!=0){
		inorder(tree[t].lch);
		printf("%d ",t);
		inorder(tree[t].rch);
	}
}

void sucorder(int t){
	if(t!=0){
		sucorder(tree[t].lch);
		sucorder(tree[t].rch);
		printf("%d ",t);
	
	}
}

int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int lch,rch;
		cin>>lch>>rch;
		tree[i].lch = lch;
		tree[i].rch = rch;
		tree[lch].ptr = i;
		tree[rch].ptr = i;
	}
	
	int root = 1;
	for(int i=1;i<=n;i++){
		if(tree[i].ptr == 0){
			root = i;
			break;
		}
	}
	
	preorder(root);
	printf("\n");
	inorder(root);
	printf("\n");
	sucorder(root);
	
	return 0;
	
	
}
